/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_SSAP_SERVER_IMPL_H
#define SLE_SSAP_SERVER_IMPL_H

#include <condition_variable>
#include <mutex>
#include <chrono>
#include "sle_utils_observer_list.h"
#include "i_sle_ssap_server.h"
#include "sle_ssap_server_interface.h"
#include "sle_ssap_server_callback_stub.h"
#include "sle_ssap_server_parcel.h"
#include "sle_ssap_common_parcel.h"
#include "sle_host_interface.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
class SleSsapServer::SleSsapServerImpl {
public:
    SleSsapServerImpl(const SleUuid &uuid);
    ~SleSsapServerImpl();
    SleErrorCode CheckOperation();

    class SleSsapServerCallbackStubImpl;
    sptr<SleSsapServerCallbackStubImpl> callbackImpl_ = nullptr;

    uint8_t serverId_;
    Uuid serverUuid_;
    bool isRegistered_;
    sptr<ISleSsapServer> proxy_ = nullptr;
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
    SleObserverList<SleSsapServerCallback> callbacks_;
    std::condition_variable conditionVariable_;
    bool ready_ = false;
    std::mutex mtx_;
    std::mutex mutexProxy_;
    SleErrorCode errCode_ = SLE_ERR_UNKNOWN;

private:
    void GetProxy();
    void ResetService(const wptr<IRemoteObject> &remote);
};

class SleSsapServer::SleSsapServerImpl::SleSsapServerCallbackStubImpl : public SleSsapServerCallbackStub {
public:
    SleSsapServerCallbackStubImpl(SleSsapServer::SleSsapServerImpl &impl) : impl_(impl) {}
    ~SleSsapServerCallbackStubImpl() = default;

    void OnStartService(uint8_t serverId, uint16_t handle, uint32_t errCode) override
    {
        if (impl_.serverId_ != serverId) {
            return;
        }
        impl_.errCode_ = (SleErrorCode)errCode;
        impl_.ready_ = true;
        impl_.conditionVariable_.notify_one();
    }

    void OnAllServiceDelete(uint8_t serverId, uint32_t errCode) override
    {
        if (impl_.serverId_ != serverId) {
            return;
        }
        impl_.errCode_ = (SleErrorCode)errCode;
        impl_.ready_ = true;
        impl_.conditionVariable_.notify_one();
    }

    void OnReadRequest(uint8_t serverId, uint16_t connectId,
        const SsapServerReqReadCbParam &param, uint32_t errCode) override
    {
        if (impl_.serverId_ != serverId) {
            return;
        }
        SsapsReqReadCbParam params;
        param.ConvertToSsapsReqReadCbParam(params);
        impl_.callbacks_.ForEach([connectId, &params, errCode](std::shared_ptr<SleSsapServerCallback> callback) {
            callback->OnReadRequest(connectId, params, (SleErrorCode)errCode);
        });
    }

    void OnWriteRequest(uint8_t serverId, uint16_t connectId,
        const SsapServerReqWriteCbParam &param, uint32_t errCode) override
    {
        if (impl_.serverId_ != serverId) {
            return;
        }
        SsapsReqWriteCbPara params;
        param.ConvertToSsapsReqWriteCbPara(params);
        impl_.callbacks_.ForEach([connectId, &params, errCode](std::shared_ptr<SleSsapServerCallback> callback) {
            callback->OnWriteRequest(connectId, params, (SleErrorCode)errCode);
        });
    }

    void OnMtuInfoChanged(uint8_t serverId, uint16_t connectId,
        uint32_t mtu, uint16_t version, uint32_t errCode) override
    {
        // TODO:sdk返回serverId与注册时不同
        // if (impl_.serverId_ != serverId) {
        //     SLE_LOGD("OnMtuInfoChanged 1");
        //     return;
        // }
        SsapMtuInfo info = {
            .mtuSize = mtu,
            .version = version
        };
        impl_.callbacks_.ForEach([connectId, &info, errCode](std::shared_ptr<SleSsapServerCallback> callback) {
            callback->OnMtuInfoChanged(connectId, info, (SleErrorCode)errCode);
        });
    }

private:
    SleSsapServer::SleSsapServerImpl &impl_;
    SLE_DISALLOW_COPY_AND_ASSIGN(SleSsapServerCallbackStubImpl);
};
} // SleStandard
} // NearLink
} // OHOS

#endif //SLE_SSAP_SERVER_IMPL_H